import CommonController from '../abstract/controller/common';
import {
  IAddUserParams,
  IDeleteUserParams,
  IQueryUserParams,
  IQueryUserListParams,
  ILoginParams,
  IUpdateUserBaseInfoParams,
  IEditUserPwdParams,
  IEditUserEmailParams,
} from '../service/user';
import { IEditUserParams } from './../service/user';
import { IQueryListCommonParams } from '../abstract/interface/common';
import { roleAuthority } from '../abstract/controller/decorators';
import recordAop from '../aop/recordAop';
import { EOperationType } from '../../config/recordMap.config';
import { UserRoleEnum } from "@/constants/enum.types";
const addUserRules = {
  name: { type: 'string' },
  password: { type: 'string' },
  role: { type: 'string' },
};

const loginRules = {
  name: { type: 'string', required: false },
  email: { type: 'string', required: false },
  password: 'string',
};

const updateUserBaseInfoRules = {
  name: { type: 'string', required: true },
  nickName: { type: 'string', required: false },
  email: { type: 'string', required: true },
  phone: { type: 'string', required: true },
  sex: { type: 'string', required: false },
  belongCenter: { type: 'string', required: false },
  belongDepartment: { type: 'string', required: false },
};

const updateUserPwdRules = {
  oldPwd: { type: 'string', required: true },
  newPwd: { type: 'string', required: true },
};

const updateUserEmailRules = {
  oldEmail: { type: 'string', required: true },
  newEmail: { type: 'string', required: true },
};

export interface IAopController {
  subject: string;
  object?: string;
  success: boolean;
  requestData?: unknown;
  resultData?: unknown;
}

export default class UserController extends CommonController {
  /* private getCurrUserInfo(ctx: Context) {
    const bearerStr = ctx.header.authorization;
    // @ts-ignore
    const authToken = bearerStr.split(' ')[1] || '';
    const curUserInfo = ((this.app.jwt.decode(authToken) as unknown) as {
      userInfo: UserInfoEntity;
    }).userInfo;
    return curUserInfo;
  } */
  /**
   * 添加用户
   */
  @roleAuthority(UserRoleEnum.super_admin)
  public async addUser() {
    this.ctx.validate(addUserRules);
    const body: IAddUserParams = this.ctx.request.body;
    const addResult = await this.ctx.service.user.addUser(body);
    this.success(addResult);
  }

  /**
   * 编辑用户
   */
  @recordAop(EOperationType.EDITUSER)
  @roleAuthority(UserRoleEnum.super_admin)
  public async editUser(): Promise<IAopController> {
    this.ctx.validate({ uuid: 'string' });
    const body: IEditUserParams = this.ctx.request.body;
    const editResult = await this.ctx.service.user.editUser(body);
    const { prevData, ...restResult } = editResult;
    this.success(restResult);
    return {
      subject: '',
      object: prevData ? prevData.name : '',
      resultData: prevData,
      requestData: body,
      success: editResult.success,
    };
  }

  /**
   * 删除用户
   */
  @recordAop(EOperationType.DELUSER)
  @roleAuthority(UserRoleEnum.super_admin)
  public async deleteUser(): Promise<IAopController> {
    this.ctx.validate({ uuid: 'string' });
    const body: IDeleteUserParams = this.ctx.request.body;
    const deleteResult = await this.ctx.service.user.deleteUser(body);
    const resultData = deleteResult.data;
    deleteResult.data = null;
    this.success(deleteResult);
    return {
      subject: this.userInfo ? this.userInfo.name : '',
      object: resultData ? resultData.name : '',
      success: deleteResult.success,
    };
  }

  /**
   * 查询用户列表
   */
  public async queryUserList() {
    // @ts-ignore
    const query: IQueryUserListParams &
      Partial<IQueryListCommonParams<string>> = this.ctx.query;
    const { page = 1, pageSize = 10, ...resQuery } = query;
    const pageOptions = {
      page: Number(page),
      pageSize: Number(pageSize),
    };
    const queryResult = await this.ctx.service.user.queryUserList(
      resQuery,
      pageOptions,
    );
    this.success(queryResult);
  }

  /**
   * 查询用户
   */
  public async queryUser() {
    const query: IQueryUserParams = this.ctx.query;
    // this.ctx.validate({ uuid: 'string' }, query);
    const queryResult = await this.ctx.service.user.queryUser(query);
    this.success(queryResult);
  }

  /**
   * 注册用户
   */
  public async register() {
    this.ctx.validate(addUserRules);
    const body: IAddUserParams = this.ctx.request.body;
    const registerResult = await this.ctx.service.user.addUser(body);
    this.success(registerResult);
  }

  /**
   * 登陆用户
   */
  @recordAop(EOperationType.LOGIN)
  public async login(): Promise<IAopController> {
    this.ctx.validate(loginRules);
    const body: ILoginParams = this.ctx.request.body;
    const loginResult = await this.ctx.service.user.login(body);
    this.success(loginResult);
    return {
      subject: body.name || body.email || '',
      success: loginResult.success,
    };
  }

  /**
   * 获取当前登录用户数据
   */
  public async getUserInfo() {
    const res = await this.ctx.service.user.queryUserById({
      id: this.userInfo ? this.userInfo.id : '',
    });
    this.success(res);
  }

  /**
   * 重置用户密码为默认密码
   */
  @recordAop(EOperationType.RESETPWD)
  public async resetDefaultPwd(): Promise<IAopController> {
    this.ctx.validate({ uuid: { type: 'string' } });
    const body: { uuid: string } = this.ctx.request.body;
    const restResult = await this.ctx.service.user.resetDefaultPwd(body);
    this.success(restResult);
    return {
      subject: this.userInfo ? this.userInfo.name : '',
      object: restResult.data ? restResult.data.name : '',
      success: restResult.success,
    };
  }

  /**
   * 获取当前用户基本信息
   */
  public async queryUserBaseInfo() {
    const bearerStr = this.ctx.header.authorization;
    // @ts-ignore
    const curUserInfo = this.app.getUserInfoByToken(bearerStr, this.app);
    const res = await this.ctx.service.user.queryUserById({ id: curUserInfo.id });
    this.success(res);
  }

  /**
   * 更新当前用户基本信息
   */
  public async updateUserBaseInfo() {
    this.ctx.validate(updateUserBaseInfoRules);
    const body: IUpdateUserBaseInfoParams = {
      uuid: this.userInfo ? this.userInfo.uuid : '',
      ...this.ctx.request.body,
    };
    const res = await this.ctx.service.user.updateUserBaseInfo(body);
    this.success(res);
  }

  /**
   * 修改当前用户的密码
   */
  public async updateUserPwd() {
    this.ctx.validate(updateUserPwdRules);
    const body: IEditUserPwdParams = this.ctx.request.body;
    const res = await this.ctx.service.user.editUserPwdService({
      uuid: this.userInfo ? this.userInfo.uuid : '',
      ...body,
    });
    this.success(res);
  }

  /**
   * 修改当前用户的邮箱
   */
  public async updateUserEmail() {
    this.ctx.validate(updateUserEmailRules);
    const body: IEditUserEmailParams = this.ctx.request.body;
    const res = await this.ctx.service.user.editUserEmailService({
      uuid: this.userInfo ? this.userInfo.uuid : '',
      ...body,
    });
    this.success(res);
  }
}
